package com.lzf.yh.userService.impl;

import com.lzf.yh.commons.JsonBean;
import com.lzf.yh.dao.MessageMapper;
import com.lzf.yh.dao.OrderInfoMapper;
import com.lzf.yh.entity.Message;
import com.lzf.yh.entity.OrderInfo;
import com.lzf.yh.userService.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: Wyz
 * @Date: 2021/5/23 23:37
 * @Description:
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    /**
     * 增加信息
     * @param message
     * @return
     */
    @Override
    public JsonBean insertMessage(Message message) {
        JsonBean jsonBean = new JsonBean(-1, "添加失败，请稍后重试", null);
        if (message.getReplyuserid()==null){
            message.setReplyuserid(0);
            message.setReplyusername("无");
        }
        //状态要根据用户id和吉他name查询出来
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUserid(message.getOwnuserid());
        orderInfo.setGuitarname(message.getGuitarinfoname());
        List<OrderInfo> orderInfos = orderInfoMapper.selectByJiTaNameAndUserId(orderInfo);
        if (orderInfos.size()==0){
            message.setStarts("1");
        }else {
            message.setStarts("0");
        }
        //添加
        int insert = messageMapper.insert(message);
        if (insert>0){
            jsonBean = new JsonBean(0, "添加成功", null);
        }
        return jsonBean;
    }

    /**
     * 查询所有交流记录，先从库里查出来，然后排下序
     * @param guitarinfoname
     * @return
     */
    @Override
    public JsonBean selectAllMessage(String guitarinfoname) {
        List<Message> messages = messageMapper.selectAllMessage(guitarinfoname);
        List<Message> messageList = new ArrayList<>();
        for (int i = 0; i < messages.size() ; i++) {
            if (messages.get(i).getStarts().equals("0")){
                messages.get(i).setStarts("已买");
            }else {
                messages.get(i).setStarts("未买");
            }
            if (messages.get(i).getSeries()==1 && i==0){
                messageList.add(messages.get(i));
            }else if (messages.get(i).getSeries()==1){
                messageList.add(messages.get(i));
            }else if (messages.get(i).getSeries()==2){
                //判断是给谁回复的
                int replyuserid = messages.get(i).getReplyuserid();
                for (int j = 0; j < messageList.size() ; j++) {
                    if (replyuserid == messageList.get(j).getOwnuserid()&&messageList.get(j).getReplyuserid()==0){
                        messageList.add(j+1,messages.get(i));
                    }
                }
            }else {
                int replyuserid = messages.get(i).getReplyuserid();
                for (int j = 0; j < messageList.size() ; j++) {
                    if (replyuserid == messageList.get(j).getOwnuserid()&&messageList.get(j).getReplyuserid()!=0){
                        messageList.add(j+1,messages.get(i));
                    }
                }
            }
        }

        return new JsonBean(0,"",messageList);
    }
}
